package com.weikuang.fareast.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.AddGoodsBean;
import com.weikuang.fareast.bean.OutGoodsBean;

import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;


/**
 * 出货
 */
public class OutGoodsAdapter extends BaseRecyclerAdapter<OutGoodsBean> {
    private OnRecyclerViewItemClickListener mOnItemClickListener;
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_out_goods;
    }

    public OutGoodsAdapter() {

    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        OutGoodsBean bean = getItem(position);
        holder.getText(R.id.name).setText(bean.productName);
        holder.getText(R.id.nums).setText(bean.nums+"");
        holder.getText(R.id.tv_debtCount).setText(bean.debtCount+"");
       // holder.getText(R.id.ed_num).setText("0");




        EditText mEditText_supplementCount = holder.getView(R.id.ed_supplementCount);

        if (mEditText_supplementCount.getTag(R.id.ed_supplementCount) instanceof TextWatcher){
            mEditText_supplementCount.removeTextChangedListener(((TextWatcher)mEditText_supplementCount.getTag(R.id.ed_supplementCount)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.supplementCount == 0) {
            mEditText_supplementCount.setText("0");
        }else {
            mEditText_supplementCount.setText(bean.supplementCount + "");
        }
        mEditText_supplementCount.setSelection(mEditText_supplementCount.getText().length());
        TextWatcher textWatcher_supplement=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("0")){

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.supplementCount= Integer.valueOf(s.toString());

                }
            }
        };
        //设置tag为TextWatcher
        mEditText_supplementCount.addTextChangedListener(textWatcher_supplement);
        mEditText_supplementCount.setTag(R.id.ed_supplementCount,textWatcher_supplement);


//////////////////////////////////////////////////////////////////////////////////////////////////////////
        EditText mExceptionEditText_ed_debtCount = holder.getView(R.id.ed_quantityCount);//交货

        if (mExceptionEditText_ed_debtCount.getTag(R.id.ed_quantityCount) instanceof TextWatcher){
            mExceptionEditText_ed_debtCount.removeTextChangedListener(((TextWatcher)mExceptionEditText_ed_debtCount.getTag(R.id.ed_quantityCount)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.quantityCount <= 0) {
            mExceptionEditText_ed_debtCount.setText("");
        }else {
            mExceptionEditText_ed_debtCount.setText(bean.quantityCount + "");
        }

      //  mExceptionEditText_ed_debtCount.setText("");
        int nums = bean.nums;
        mExceptionEditText_ed_debtCount.setSelection(mExceptionEditText_ed_debtCount.getText().length());
        TextWatcher textWatcher_debtCount=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("0")){

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.quantityCount= Integer.valueOf(s.toString());

                }
            }
        };

        holder.getText(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.quantityCount = nums;
                mExceptionEditText_ed_debtCount.setText(bean.quantityCount + "");
            }
        });
        //设置tag为TextWatcher
        mExceptionEditText_ed_debtCount.addTextChangedListener(textWatcher_debtCount);
        mExceptionEditText_ed_debtCount.setTag(R.id.ed_quantityCount,textWatcher_debtCount);


    }
    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, AddGoodsBean data, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
